<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('产业地图') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('名称')" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <!-- <el-form-item :label="$t('投前项目数量')" prop="preDealNumber">
        <el-input
          v-model="queryParams.preDealNumber"
          :placeholder="$t('投前项目数量')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('投后项目数量')" prop="postDealNumber">
        <el-input
          v-model="queryParams.postDealNumber"
          :placeholder="$t('投后项目数量')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:map:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealEstateMapList" class="mapBtnDiy" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('名称')" align="center" prop="fullName" />
      <el-table-column :label="$t('投前项目数量')" align="center" prop="preDealNumber" />
      <el-table-column :label="$t('投后项目数量')" align="center" prop="postDealNumber" />
      <el-table-column :label="$t('当年更新频次')" align="center" prop="editSum">
        <template slot-scope="scope">
          <span class="link-type" @click="viewLogHistory(scope.row, 'edit')">{{ scope.row.editSum }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('浏览次数')" align="center" prop="viewSum">
        <template slot-scope="scope">
          <span class="link-type" @click="viewLogHistory(scope.row, 'view')">{{ scope.row.viewSum }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('负责人')" align="center" prop="createBy.nickName" />
      <!-- <el-table-column :label="$t('创建时间')" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column :label="$t('最后更新人')" align="center" prop="updateBy.nickName" /> -->
      <el-table-column :label="$t('修改时间')" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('状态')" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span v-if="!scope.row.isLock || scope.row.isLock === 'N'"> 可编辑</span>
          <span v-else>{{ scope.row.lockUserName }}正在编辑</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding" min-width="180">
        <template slot-scope="scope">
          <span v-if="!scope.row.isLock || scope.row.isLock === 'N'">
            <el-button
              v-if="scope.row.createBy.id === userId || isAdmin"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}</el-button>
          </span>
          <el-button
            v-if="scope.row.createBy.id === userId || isAdmin || scope.row.isBelongTeam"
            size="small"
            type="text"
            icon="el-icon-edit-outline"
            @click="designHandle(scope.row)"
          >{{ $t('编辑') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-view"
            @click="viewHandle(scope.row)"
          >{{ $t('查看') }}</el-button>
          <span v-if="(userId === scope.row.createBy.id || userId === '1')">
            <el-button
              v-if="!scope.row.isLock || scope.row.isLock === 'N'"
              v-hasPermi="['deal:map:edit']"
              size="small"
              type="text"
              icon="el-icon-lock"
              @click="lockHandle(scope.row)"
            >{{ $t('锁定') }}</el-button>
            <el-button
              v-else
              v-hasPermi="['deal:map:edit']"
              size="small"
              type="text"
              icon="el-icon-unlock"
              @click="unLockHandle(scope.row)"
            >{{ $t('解锁') }}</el-button>
          </span>
          <el-button
            v-if="scope.row.createBy.id === userId || isAdmin"
            size="small"
            type="text"
            icon="el-icon-user"
            @click="teamHandle(scope.row)"
          >{{ $t('团队成员') }}</el-button>
          <el-button
            v-hasPermi="['deal:map:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产业地图对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body width="30%">
      <dealEstateMapForm ref="dealEstateMapForm" />
    </el-dialog>
    <el-dialog title="产业地图团队权限" :visible.sync="teamFormOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body width="80%">
      <dealEstateMapPermitList ref="dealEstateMapPermitList" />
    </el-dialog>
    <el-dialog title="操作日志" :visible.sync="logOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body width="80%">
      <dealEstateMapLogList ref="dealEstateMapLogList" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealEstateMap, delDealEstateMap, exportDealEstateMap, operateInfo } from '@/api/deal/map/dealEstateMap'
import dealEstateMapForm from './dealEstateMapForm.vue'
import dealEstateMapPermitList from './dealEstateMapPermitList.vue'
import dealEstateMapLogList from './dealEstateMapLogList.vue'
export default {
    components: {
        dealEstateMapForm,
        dealEstateMapPermitList,
        dealEstateMapLogList
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 产业地图表格数据
          dealEstateMapList: [],
          userId: this.$store.state.user.userId,
          // 弹出层标题
          title: '产业地图',
          // 是否显示弹出层
          formOpen: false,
          teamFormOpen: false,
          logOpen: false,
          // 查询参数
          queryParams: {
              fullName: undefined,
              preDealNumber: undefined,
              postDealNumber: undefined,
              dataJson: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 是否为管理员
          isAdmin: this.$store.state.user.userId === '1'
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询产业地图列表 */
    getList() {
        this.loading = true
        listDealEstateMap(this.queryParams).then(response => {
            this.dealEstateMapList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            fullName: undefined,
            preDealNumber: undefined,
            postDealNumber: undefined,
            dataJson: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealEstateMapForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealEstateMapForm.init(row)
        })
    },
    teamHandle(row) {
        this.teamFormOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealEstateMapPermitList.getList(row)
        })
    },
    viewLogHistory(row, type) {
      this.logOpen = true
      this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            const data = {
              mapId: row.id,
              operType: type
            }
            this.$refs.dealEstateMapLogList.getList(data)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealEstateMap(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 锁定 */
    lockHandle(row) {
        this.$confirm(this.$t('是否确认锁定?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return operateInfo(row.id, 'Y')
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('锁定成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 解锁 */
    unLockHandle(row) {
        this.$confirm(this.$t('是否确认解锁?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return operateInfo(row.id, 'N')
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('解锁成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportDealEstateMap(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    // 设计产业地图
    designHandle(row) {
      const routeData = this.$router.resolve({ path: '/designMap', query: { id: row.id, mode: 'edit' }})
      window.open(routeData.href, '_blank')
    },
    // 查看产业地图
    viewHandle(row) {
      const routeData = this.$router.resolve({ path: '/designMap', query: { id: row.id, mode: 'readonly' }})
      window.open(routeData.href, '_blank')
    }
  }
}
</script>
<style>
.mapBtnDiy .el-button + .el-button {
    margin-left: 0px;
  }
</style>
